/**
 * 
 */
package com.firebird.oms.web.dwr.sm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.firebird.base.domain.KeyLabel;
import com.firebird.base.domain.Page;
import com.firebird.oms.constants.AuthConstants;
import com.firebird.oms.domain.sm.TismArea;
import com.firebird.oms.domain.sm.TismCompany;
import com.firebird.oms.exception.OmsRuntimeException;
import com.firebird.oms.service.sm.CompanyService;
import com.firebird.oms.user.UserDetails;
import com.firebird.oms.web.base.BaseDwr;

/**
 * @author qiuxj 创建于 2009-11-26
 *
 */
public class AreaDwr extends BaseDwr {
	
	private CompanyService companyService;
	
	/**
	 * 创建责任区域
	 * @param smArea
	 */
	public void createArea(TismArea smArea){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
			throw new OmsRuntimeException("权限不足。");
		}
		Date date = new Date();
		smArea.setCreateTime(date);
		smArea.setModifyTime(date);
		smArea.setStaffId(user.getStaffId());
		companyService.createArea(smArea);
	}
	
	/**
	 * 更新责任区域
	 * @param smArea
	 */
	public void updateArea(TismArea smArea){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
			throw new OmsRuntimeException("权限不足。");
		}
		smArea.setModifyTime(new Date());
		companyService.updateArea(smArea);
	}
	
	/**
	 * 获取责任区域
	 * @param areaId
	 * @return
	 */
	public TismArea getArea(int areaId){
		return companyService.getArea(areaId);
	}
	
	/**
	 * 获取责任区域列表
	 * @param filter
	 * @return
	 */
	public List<TismArea> findArea(Map<String, String> filter){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() != AuthConstants.WORKER_LEVEL_ADMIN){
			if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
				filter.put("staffId", user.getUpStaffId().toString());
				
			}else{
				filter.put("staffId", user.getStaffId().toString());
			}
		}
		
		List<TismArea> areas = companyService.findArea(filter);
		
		for(TismArea area : areas){
			TismCompany company = companyService.getCompany(area.getCompanyId());
			area.setCompanyName(company != null?company.getCompanyName():"");
		}
		
		return areas;
	}
	
	/**
	 * 获取分页责任区域列表
	 * @param filterMap
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageArea(Map<String, String> filterMap, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() != AuthConstants.WORKER_LEVEL_ADMIN){
			if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
				filterMap.put("staffId", user.getUpStaffId().toString());
			}else{
				filterMap.put("staffId", user.getStaffId().toString());
			}
		}
		Page page = companyService.pageArea(filterMap, likeMap, sortMap, pageNo, pageSize);
		
		for(TismArea area : (List<TismArea>)page.getResult()){
			TismCompany company = companyService.getCompany(area.getCompanyId());
			area.setCompanyName(company != null?company.getCompanyName():"");
		}
		
		return page;
	}
	
	/**
	 * 获取片区keyLabel
	 * @param filter
	 * @return
	 */
	public List<KeyLabel> getAreaKls(Map<String, String> filter) {
		List<TismArea> areas = this.findArea(filter);
		
		List<KeyLabel> kls = new ArrayList<KeyLabel>(0);
		for(TismArea area : areas){
			kls.add(new KeyLabel(area.getAreaId().toString(), area.getAreaName()));
		}
		return kls;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

}
